package com.financing.wap.web.controller.adminapi;


import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 后台登录
 *
 */
@Controller("loginController")
@RequestMapping("/adminapi")
public class LoginController extends BaseController {
	Logger logger = Logger.getLogger(getClass());
	

	@RequestMapping(value="/login.shtml")
    @ResponseBody
	public ResultMap login(HttpServletRequest request, HttpServletResponse response) {

        ResultMap result = new ResultMap();

        try {
            logger.info("in login begin .......");
            String userName = request.getParameter("username");
            String password = request.getParameter("password");

            //参数  默认
            if (StringUtil.isNullOrEmptyStr(userName, password)) {
                result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
                result.setStatus(ResultMap.STATUS.FAILE);
                return result;
            }
            if (!userName.equals("admin") || !password.equals("123456")) {
                result.setStatusCode(ResultMap.STATUS_CODE_PWS_ERROR);
                result.setStatus(ResultMap.STATUS.FAILE);
                return result;

            }

            Session session = getSession(true);
            session.setLogin(true);
            session.setAttribute("name",userName);


            result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
            result.setStatus(ResultMap.STATUS.SUCCESS);

            logger.info("in login  end .......");
        } catch (Exception e) {
            log.error("in login error!", e);
            result.setStatus(ResultMap.STATUS.ERROR);
            result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
        }

        return result;

    }

    @RequestMapping(value="/queryLoginStatus.shtml")
    @ResponseBody
    public ResultMap queryLoginStatus(HttpServletRequest request, HttpServletResponse response) {
        ResultMap result = new ResultMap();
        try{
            Session session = getSession(true);
            if(session != null && session.getLogin() ){
                result.setAttribute("loginStatus", true);
            }else{
                result.setAttribute("loginStatus", false);
            }
            result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
            result.setStatus(ResultMap.STATUS.SUCCESS);
        }catch(Exception e){
            log.error("queryLoginStatus error!",e);
            result.setStatus(ResultMap.STATUS.ERROR);
            result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
        }
        return result;
    }

    /**
     *
     * @Title: logout
     * @Description: 登出
     * @param   request
     * @param   response
     * @return ResultMap
     * @throws
     */
    @RequestMapping("/logout.shtml")
    @ResponseBody
    public ResultMap logout(HttpServletRequest request,
                            HttpServletResponse response){
        ResultMap result = new ResultMap();
        try{

            request.getSession().invalidate();

            result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
            result.setStatus(ResultMap.STATUS.SUCCESS);
        }catch(Exception e){
            log.error("logout error!",e);
            result.setStatus(ResultMap.STATUS.ERROR);
            result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
        }
        return result;
    }
}
